package com.csust.automaticteach.app.rag.config;

import com.csust.automaticteach.app.rag.transformer.MyKeywordEnricher;
import com.csust.automaticteach.app.rag.transformer.MySummaryEnricher;
import org.springframework.ai.chroma.vectorstore.ChromaApi;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName LearnAppVectorStoreConfig
 * @Description 向量存储配置
 * @Author 钰玟
 * @Date 2025/6/13 上午11:53
 * @Version 1.0
 **/
@Configuration
public class LearnAppVectorStoreConfig {
    private final MyKeywordEnricher myKeywordEnricher;
    private final MySummaryEnricher mySummaryEnricher;
    private final ChromaApi chromaApi;

    public LearnAppVectorStoreConfig(MyKeywordEnricher myKeywordEnricher, MySummaryEnricher mySummaryEnricher, ChromaApi chromaApi) {
        this.myKeywordEnricher = myKeywordEnricher;
        this.mySummaryEnricher = mySummaryEnricher;
        this.chromaApi = chromaApi;
    }
    @Bean
    CommandLineRunner ingestTermOfServiceToVectorStore(VectorStoreDelegate vectorStoreDelegate) {
        return args -> {
            VectorStoreInitializer initializer = new VectorStoreInitializer(myKeywordEnricher, mySummaryEnricher, chromaApi);
            //现在默认为 内存的
            initializer.init(vectorStoreDelegate.getVectorStore("chroma"));
        };
    }
    @Bean
    VectorStore learnAppVectorStore(EmbeddingModel dashscopeEmbeddingModel) {
        return SimpleVectorStore.builder(dashscopeEmbeddingModel).build();
    }
    /**
     * 提供基于内存的向量存储（SimpleVectorStore）
     * <p>
     * 依赖 EmbeddingModel（自动注入，Alibaba 的嵌入模型）
     * @param embeddingModel 向量化模型
     * @return VectorStore
     */
    @Bean
    public VectorStore simpleVectorStore(
            @Qualifier("dashscopeEmbeddingModel") EmbeddingModel embeddingModel
    ) {

        return SimpleVectorStore.builder(embeddingModel).build();
    }
    @Bean
    public VectorStoreDelegate vectorStoreDelegate(
            @Qualifier("simpleVectorStore") VectorStore simpleVectorStore,
            @Qualifier("chromaVectorStore") @Autowired(required = false) VectorStore chromaVectorStore
    ) {

        return new VectorStoreDelegate(simpleVectorStore, chromaVectorStore);
    }
}
